/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.commons.validator;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorException;
import org.apache.commons.validator.ValidatorResources;
import org.apache.commons.validator.ValidatorResult;
import org.apache.commons.validator.ValidatorResults;
import org.apache.commons.validator.util.ValidatorUtils;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

/**
 * https://commons.apache.org/proper/commons-validator/apidocs/org/apache/commons/validator/package-summary.html#package_description
 * 
 * Validator Example
 * 
 * Pluggable Validator Example
 * 
 */
public class GenericBean {
    
    /**
     * Pluggable Validator Example
     * @param bean
     * @param field
     * @return
     */
    public static boolean validateRequired(Object bean, Field field) {
        String value = ValidatorUtils.getValueAsString(bean, field.getProperty());
        return GenericValidator.isBlankOrNull(value);
    }
    
    /**
     * Validator Example
     * @param args
     */
    public static void main(String[] args) throws IOException, SAXException, ValidatorException {
        
        try (InputStream in = GenericBean.class.getResourceAsStream("/META-INF/validation/commons-validator/validator-name-required.xml")) {
            // Create an instance of ValidatorResources to initialize from an xml file.
            ValidatorResources resources = new ValidatorResources(in);
            // Create bean to run test on.
            SecuredShopPersistableCustomer customer = new SecuredShopPersistableCustomer();
            customer.setUsername("haha");
            // Construct validator based on the loaded resources and the form key
            Validator validator = new Validator(resources, "customerForm");
            // add the name bean to the validator as a resource
            // for the validations to be performed on.
            validator.setParameter(Validator.BEAN_PARAM, customer);
            // Get results of the validation.
            ValidatorResults results = validator.validate();
            // throws ValidatorException (catch clause not shown here)
            ValidatorResult userNameResult = results.getValidatorResult("username");
            ValidatorResult passwordResult = results.getValidatorResult("password");
            System.out.println("username - result : " + userNameResult.getResult("required"));
            System.out.println("password - result : " + passwordResult.getResult("required"));
        }
    
    
    }
    
}



